/**
 * Copyright (c) 2013 An Yaming,  All Rights Reserved
 */
package so.shanku.essential.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import aym.util.json.JsonMap;
import so.shanku.essential.R;
import so.shanku.essential.adapter.ProductParamsAdapter;
import java.util.List;

/**
 * 展示商品参数的布局
 *
 * @author 亚明
 */
public class LayoutProductInfoParams1 extends LinearLayout {

    private Context context;
    /**
     * 参数的标题
     */
    private TextView tv_title;
    /**
     * 参数信息的展示
     */
    private GridViewNoScroll pipg_info;
    /**
     * 展示的参数信息
     */
    private List<JsonMap<String, Object>> data;
    /**
     * 当前选中的参数的view的索引id
     */
    private int tv_position;
    /**
     * 当前的参数有改变的事件
     */
    private MyOnItemClickListener itemClickListener;

    /**
     * 当前的参数有改变的事件
     */
    public void setItemClickListener(MyOnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    /**
     * 当前选中的参数的view的索引id
     */
    public int getTv_position() {
        return tv_position;
    }

    /**
     * 展示的参数信息
     */
    public List<JsonMap<String, Object>> getData() {
        return data;
    }

    public LayoutProductInfoParams1(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public LayoutProductInfoParams1(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();

    }

    public LayoutProductInfoParams1(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_product_info_params, this);
        tv_title = (TextView) findViewById(R.id.l_p_i_tv_pro_param);
        pipg_info = (GridViewNoScroll) findViewById(R.id.l_p_i_fgl_pro_param);
        pipg_info.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (tv_position == position) {
                    return;
                }
                view.setSelected(true);
                if(tv_position>-1) {
                    View v = parent.getChildAt(tv_position);
                    v.setSelected(false);
                }

                tv_position = position;
                if (null != itemClickListener) {
                    itemClickListener.itemClick(view, position);
                }

            }
        });
    }

    /**
     * 设定分类的标题
     *
     * @param title
     */

    public void setTitle(String title) {
        this.tv_title.setText(title);
    }

    /**
     * 设定这个分类下的数据
     */
    public void setParamsData(List<JsonMap<String, Object>> data,
                              int defaultSelectPosition) {
        this.data = data;
        ProductParamsAdapter simpleGridAdater = new ProductParamsAdapter(context, data);
        pipg_info.setAdapter(simpleGridAdater);
        tv_position = simpleGridAdater.getCheckedPosition();
        simpleGridAdater.notifyDataSetChanged();
    }

    //e

    /**
     * 参数改变的事件
     *
     * @author 亚明
     */
    public interface MyOnItemClickListener {
        void itemClick(View tv, int position);
    }
}
